package com.yzf.wisecoder.controller;

import com.yzf.wisecoder.wisecodercommon.response.Response;
import com.yzf.wisecoder.wisecodercommon.utils.BeanCopyUtils;
import com.yzf.wisecoder.wisecodercommon.utils.SecurityUtils;
import com.yzf.wisecoder.wisecodercommon.utils.ValidatorUtils;
import com.yzf.wisecoder.wisecodercommon.vo.request.ProjectUserReq;
import com.yzf.wisecoder.wisecodercommon.vo.response.ProjectUserResp;
import com.yzf.wisecoder.wisecoderservice.service.ProjectUserService;
import com.yzf.wisecoder.wisecoderstorage.mysql.entity.ProjectUserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author jianghuiyong
 * @email jianghuiyong@yunzhangfang.com
 * @date 2020-05-06 17:43:12
 */
@RestController
@RequestMapping("sys/projectuser")
@AllArgsConstructor
@Api(value = "用户项目关系控制", tags = "用户项目关系控制")
public class ProjectUserController {

    private final ProjectUserService projectUserService;

    /**
     * 保存用户项目关系
     *
     * @param projectUserReq
     * @return
     */
    @PostMapping("saveOrUpdate")
    @ApiOperation(value = "保存或修改用户项目关系")
    public Response saveOrUpdate(@RequestBody ProjectUserReq projectUserReq) {
        ValidatorUtils.validateEntity(projectUserReq);
        projectUserService.saveProjectUser(projectUserReq);
        return Response.buildSuccessResponse();
    }

    /**
     * 删除用户项目关系
     *
     * @param userId
     * @param projectId
     * @return
     */
    @DeleteMapping("remove")
    @ApiOperation(value = "删除用户项目关系")
    public Response remove(@RequestParam("userId") Long userId, @RequestParam("projectId") Long projectId) {
        projectUserService.removeProjectUser(userId, projectId);
        return Response.buildSuccessResponse();
    }

    /**
     * 查询项目关联用户
     *
     * @param projectId
     * @return
     */
    @GetMapping("/queryProjectUsers")
    @ApiOperation(value = "查询项目关联用户")
    public Response<List<ProjectUserResp>> queryProjectUsers(@RequestParam Long projectId) {
        List<ProjectUserEntity> list = projectUserService.queryByProjectId(projectId, SecurityUtils.getTenantId());
        List<ProjectUserResp> projectUserResps = BeanCopyUtils.copyList(list, ProjectUserResp.class);
        return Response.buildSuccessResponseWithData(projectUserResps);
    }
}
